package com.lehecai.core.lottery.fetcher.jclq.impl.worker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lehecai.core.lottery.JclqSPType;
import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.jclq.AbstractJclqDynamicInstantSpFetchWorker;
import com.lehecai.core.lottery.fetcher.jclq.JclqDynamicInstantSpItem;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreHttpUtils;
import com.lehecai.core.util.CoreJSONUtils;

/**
 * 通用竞彩篮球浮动奖金即时sp抓取实现类
 * @author qatang
 *
 */
public class CommonJclqDynamicInstantSpFetchWorkerDataAPI extends AbstractJclqDynamicInstantSpFetchWorker{
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	private static final String URL_PENGINE_API = "http://data.lecai.com/api/simple/sporttery/jclq/immediate/sp.do";

    @Override
	public List<JclqDynamicInstantSpItem> fetchJclqSp(String phaseNo, LotteryType lotteryType, String terminalId){
		if (StringUtils.isBlank(phaseNo)) {
			logger.error("竞彩篮球抓取官方sp时，officialDate为空");
			return null;
		}
		phaseNo = phaseNo.replace("-", "");
		
		List<String> list = null;
		try {
            Map<String, String> requestMap = new HashMap<String, String>();
            requestMap.put("phaseNo", phaseNo);
            requestMap.put("l", String.valueOf(lotteryType.getValue()));
            requestMap.put("p", "1");
			list = CoreHttpUtils.getUrl(URL_PENGINE_API, requestMap, CharsetConstant.CHARSET_UTF8, 100000);
		} catch (Exception e) {
			logger.error("竞彩篮球抓取官方浮动奖金即时sp失败", e);
			return null;
		}
		if (list == null || list.size() == 0) {
			logger.error("竞彩篮球抓取官方浮动奖金即时sp失败");
			return null;
		}
		String matches = list.get(0);
		if (matches == null || "".equals(matches)) {
			logger.error("未获取到竞彩篮球官方浮动奖金即时sp");
			return null;
		}
		
		JSONObject dataJson = null;
		try {
			dataJson = JSONObject.fromObject(matches);
		} catch (Exception e) {
			logger.error("竞彩篮球抓取官方浮动奖金即时sp失败", e);
			return null;
		}
		
		if (dataJson == null) {
			logger.error("未获取到竞彩篮球官方浮动奖金即时sp");
		}
		
		int code = dataJson.getInt("code");
		if(code == -1){
			logger.error("竞彩篮球抓取官方浮动奖金即时spsp出错");
			return null;
		}
		
		JSONArray jsonArray = dataJson.getJSONArray("data");
		if (jsonArray == null) {
			logger.error("未获取到竞彩篮球官方浮动奖金即时sp");
			return null;
		}
		List<JclqDynamicInstantSpItem> jczqDynamicInstantSpItems = new ArrayList<JclqDynamicInstantSpItem>();
		for (Iterator<?> iterator = jsonArray.iterator(); iterator.hasNext();) {
			JSONObject object = (JSONObject) iterator.next();
			if (object == null) {
				return null;
			}

            // 如果指定了彩期，忽略掉返回结果中的非指定彩期的结果
            if (StringUtils.isNotBlank(phaseNo)) {
                String fetchedPhase = CoreJSONUtils.getString(object, "od");
                if (!fetchedPhase.equals(phaseNo)) {
                    continue;
                }
            }

            JclqDynamicInstantSpItem item = new JclqDynamicInstantSpItem();
			
            item.setMatchNum(CoreJSONUtils.getString(object, "mnu"));
			item.setOfficialNum(CoreJSONUtils.getInt(object, "on"));
			item.setOfficialDate(CoreJSONUtils.getDate(object, "od", "yyyyMMdd"));
			item.setMatchName(CoreJSONUtils.getString(object, "mn"));
			item.setMatchDate(CoreJSONUtils.getDate(object, "matchDate", "yyyy-MM-dd HH:mm:ss"));
			item.setHomeTeam(CoreJSONUtils.getString(object, "ht"));
			item.setAwayTeam(CoreJSONUtils.getString(object, "at"));
			
			JSONObject spObj = object.getJSONObject("sp");
			
			Map<JclqSPType, String> map = new HashMap<JclqSPType, String>();
			if (lotteryType.getValue() == LotteryType.JCLQ_SF.getValue()) {
				map.put(JclqSPType.SF_S, CoreJSONUtils.getString(spObj, "w"));
				map.put(JclqSPType.SF_F, CoreJSONUtils.getString(spObj, "l"));
			} else if (lotteryType.getValue() == LotteryType.JCLQ_RFSF.getValue()) {
				map.put(JclqSPType.RFSF_S, CoreJSONUtils.getString(spObj, "w"));
				map.put(JclqSPType.RFSF_F, CoreJSONUtils.getString(spObj, "l"));
				//map.put(JclqSPType.RFSF_RF, CoreJSONUtils.getString(spObj, "dh"));
			} else if (lotteryType.getValue() == LotteryType.JCLQ_SFC.getValue()) {
				map.put(JclqSPType.SFC_KS_1_5, CoreJSONUtils.getString(spObj, "a1t5"));
				map.put(JclqSPType.SFC_ZS_1_5, CoreJSONUtils.getString(spObj, "h1t5"));
				map.put(JclqSPType.SFC_KS_6_10, CoreJSONUtils.getString(spObj, "a6t10"));
				map.put(JclqSPType.SFC_ZS_6_10, CoreJSONUtils.getString(spObj, "h6t10"));
				map.put(JclqSPType.SFC_KS_11_15, CoreJSONUtils.getString(spObj, "a11t15"));
				map.put(JclqSPType.SFC_ZS_11_15, CoreJSONUtils.getString(spObj, "h11t15"));
				map.put(JclqSPType.SFC_KS_16_20, CoreJSONUtils.getString(spObj, "a16t20"));
				map.put(JclqSPType.SFC_ZS_16_20, CoreJSONUtils.getString(spObj, "h16t20"));
				map.put(JclqSPType.SFC_KS_21_25, CoreJSONUtils.getString(spObj, "a21t25"));
				map.put(JclqSPType.SFC_ZS_21_25, CoreJSONUtils.getString(spObj, "h21t25"));
				map.put(JclqSPType.SFC_KS_26, CoreJSONUtils.getString(spObj, "a26t"));
				map.put(JclqSPType.SFC_ZS_26, CoreJSONUtils.getString(spObj, "h26t"));
			} else if (lotteryType.getValue() == LotteryType.JCLQ_DXF.getValue()) {
				map.put(JclqSPType.DXF_S, CoreJSONUtils.getString(spObj, "b"));
				map.put(JclqSPType.DXF_F, CoreJSONUtils.getString(spObj, "s"));
				//map.put(JclqSPType.DXF_YS, CoreJSONUtils.getString(spObj, "dpres"));
			}
			
			item.setSpmap(map);
			jczqDynamicInstantSpItems.add(item);
		}
		return jczqDynamicInstantSpItems;
	}

	@Override
	public List<JclqDynamicInstantSpItem> fetchJclqSp(String phaseNo,
			LotteryType lotteryType) {
		return fetchJclqSp(phaseNo, lotteryType, null);
	}
}